package com.ks.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ks.api.entity.CultivationLevel;
import com.ks.api.service.CultivationLevelService;

@RestController
@RequestMapping("/cultivation")
public class CultivationLevelController {

    @Autowired
    private CultivationLevelService cultivationLevelService;
    
    @GetMapping("/levels")
    public ResponseEntity<List<CultivationLevel>> getAllLevels() {
        return ResponseEntity.ok(cultivationLevelService.findAllLevels());
    }
    
    @GetMapping("/levels/{id}")
    public ResponseEntity<CultivationLevel> getLevelById(@PathVariable Long id) {
        CultivationLevel level = cultivationLevelService.findLevelById(id);
        if (level != null) {
            return ResponseEntity.ok(level);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/levels/score/{score}")
    public ResponseEntity<CultivationLevel> getLevelByScore(@PathVariable Integer score) {
        CultivationLevel level = cultivationLevelService.findLevelByScore(score);
        if (level != null) {
            return ResponseEntity.ok(level);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/levels/current")
    public ResponseEntity<CultivationLevel> getCurrentLevel(@RequestParam Integer score) {
        CultivationLevel level = cultivationLevelService.getCurrentLevel(score);
        if (level != null) {
            return ResponseEntity.ok(level);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/levels/next")
    public ResponseEntity<CultivationLevel> getNextLevel(@RequestParam Integer score) {
        CultivationLevel level = cultivationLevelService.getNextLevel(score);
        if (level != null) {
            return ResponseEntity.ok(level);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/levels/major/{majorStage}")
    public ResponseEntity<List<CultivationLevel>> getLevelsByMajorStage(@PathVariable String majorStage) {
        return ResponseEntity.ok(cultivationLevelService.findLevelsByMajorStage(majorStage));
    }
    
    @PostMapping("/levels")
    public ResponseEntity<CultivationLevel> addLevel(@RequestBody CultivationLevel level) {
        boolean success = cultivationLevelService.addLevel(level);
        if (success) {
            return ResponseEntity.ok(level);
        }
        return ResponseEntity.badRequest().build();
    }
    
    @PutMapping("/levels/{id}")
    public ResponseEntity<CultivationLevel> updateLevel(@PathVariable Long id, @RequestBody CultivationLevel level) {
        level.setId(id);
        boolean success = cultivationLevelService.updateLevel(level);
        if (success) {
            return ResponseEntity.ok(level);
        }
        return ResponseEntity.notFound().build();
    }
    
    @DeleteMapping("/levels/{id}")
    public ResponseEntity<Void> deleteLevel(@PathVariable Long id) {
        boolean success = cultivationLevelService.deleteLevelById(id);
        if (success) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.notFound().build();
    }
} 